package org.crazyrunner.module.sys.entity;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;

import org.crazyrunner.common.entity.BaseEntity;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.NotFound;
import org.hibernate.annotations.NotFoundAction;
import org.hibernate.annotations.Where;
@Entity
@DynamicInsert @DynamicUpdate
@Table(name="T_MENU")
public class Menu extends BaseEntity<Menu>{
	public static final String TARGET_BLANK = "_blank";
	public static final String TARGET_SELF  = "_self";
	public static final String TARGET_LEFTFRAME= "leftFrame";
	public static final String TARGET_MAINFRAME="mainFrame";
	public static final String TARGET_PARENT= "_parent";
	public static final String TARGET_TOP   = "_top";
	
	private Menu parent;	// 父级菜单
	private String parentIds; // 所有父级编号
	private String name; 	// 名称
	private String href; 	// 链接
	private String target; 	// 目标（ mainFrame、_blank、_self、_parent、_top）
	private String icon; 	// 图标
	private Integer sort; 	// 排序
	private String isShow; 	// 是否在菜单中显示（1：显示；0：不显示）
	private String isActiviti; 	// 是否同步到工作流（1：同步；0：不同步）
	private String permission; // 权限标识

	private List<Menu> childList =new ArrayList<Menu>();// 拥有子菜单列表
	private List<Role> roleList = new ArrayList<Role>(); // 拥有角色列表
	
	@ManyToOne(fetch = FetchType.LAZY)
	@JoinColumn(name="parent_id")
	@NotFound(action = NotFoundAction.IGNORE)
	public Menu getParent() {
		return parent;
	}
	public void setParent(Menu parent) {
		this.parent = parent;
	}
	
	public String getParentIds() {
		return parentIds;
	}
	public void setParentIds(String parentIds) {
		this.parentIds = parentIds;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getHref() {
		return href;
	}
	public void setHref(String href) {
		this.href = href;
	}
	public String getTarget() {
		return target;
	}
	public void setTarget(String target) {
		this.target = target;
	}
	public String getIcon() {
		return icon;
	}
	public void setIcon(String icon) {
		this.icon = icon;
	}
	public Integer getSort() {
		return sort;
	}
	public void setSort(Integer sort) {
		this.sort = sort;
	}
	public String getIsShow() {
		return isShow;
	}
	public void setIsShow(String isShow) {
		this.isShow = isShow;
	}
	public String getIsActiviti() {
		return isActiviti;
	}
	public void setIsActiviti(String isActiviti) {
		this.isActiviti = isActiviti;
	}
	public String getPermission() {
		return permission;
	}
	public void setPermission(String permission) {
		this.permission = permission;
	}
	
	@OneToMany(mappedBy = "parent", fetch=FetchType.LAZY)
	@Where(clause="delFlag='"+DEL_FLAG_NORMAL+"'")
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy(value="sort") 
	@NotFound(action = NotFoundAction.IGNORE)
	//@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Menu> getChildList() {
		return childList;
	}
	public void setChildList(List<Menu> childList) {
		this.childList = childList;
	}
	
	@ManyToMany(mappedBy = "menuList", fetch=FetchType.LAZY)
	@Where(clause="delFlag='"+DEL_FLAG_NORMAL+"'")
	@Fetch(FetchMode.SUBSELECT)
	@OrderBy("id") 
	@NotFound(action = NotFoundAction.IGNORE)
	//@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
	public List<Role> getRoleList() {
		return roleList;
	}
	
	public void setRoleList(List<Role> roleList) {
		this.roleList = roleList;
	}
	
	
}